package com.business.sys.service.impl;


import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.business.sys.entity.Area;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.business.common.utils.PageUtils;
import com.business.common.dto.R;

import com.business.sys.entity.IndustryCategory;
import com.business.sys.mapper.IndustryCategoryMapper;
import com.business.sys.service.IndustryCategoryService;

import javax.xml.transform.Result;


/**
 * 行业分类表:业务接口实现类
 *
 * @author chenjiehong
 * @date 2019-11-29 14:24:28
 * @since 1.0
 */
@Service
public class IndustryCategoryServiceImpl implements IndustryCategoryService {

    @Autowired
    private IndustryCategoryMapper industryCategoryMapper;


    @Override
    @Transactional(readOnly = true)
    public List<IndustryCategory> query(IndustryCategory industryCategory) {
        if (industryCategory.getState() == null) {
            industryCategory.setState(1);
        }
        EntityWrapper<IndustryCategory> wrapper = new EntityWrapper<>(industryCategory);
        return industryCategoryMapper.selectList(wrapper);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Map<String, Object>> queryAll() {
        return getSublist(0);
    }

    /**
     * 获取子列表
     *
     * @param parentId
     * @return
     */
    private List<Map<String, Object>> getSublist(int parentId) {
        Wrapper<IndustryCategory> wrapper = new EntityWrapper<>();
        wrapper.setSqlSelect("id,name,code").eq("state", 1);
        wrapper.eq("parent_id", parentId);
        List<Map<String, Object>> sublist = industryCategoryMapper.selectMaps(wrapper);
        if(parentId == 0){
            for (Map<String, Object> m : sublist) {
                Integer id = Integer.parseInt(m.get("id") + "");
                List sublist2 = getSublist(id);
                if (sublist2 != null && sublist2.size() > 0) {
                    m.put("sublist", sublist2);
                }
            }
        }
        return sublist;
    }
}